package com.tj.shopping.service.impl;

import com.tj.shopping.entity.User;
import com.tj.shopping.mapper.UserMapper;
import com.tj.shopping.service.IUserService;
import com.tj.shopping.util.MybatisUtil;
import com.tj.shopping.util.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements IUserService {

    private UserMapper mapper;

    public Map<String, Object> findall() {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(UserMapper.class);
        List<User> list2 = mapper.findall();
        Map<String,Object> map3 = new HashMap<>();
        if (null == list2 || list2.isEmpty()){
            map3.put("code",600);
            map3.put("msg","暂无数据");
        } else {
            map3.put("code",200);
            map3.put("msg","成功");
            map3.put("data",list2);

        }
        MybatisUtil.close(session);
        return map3;
    }

    @Override
    public int update(User user) {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(UserMapper.class);

        if (user.getUstate()!=null){
            if (user.getUstate()==1){
                user.setUstate(0);
            }else {
                user.setUstate(1);
            }

        }

        return mapper.update(user);
    }

    @Override
    public Map<String, Object> finds(Map<String, Object> map) {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(UserMapper.class);
        List<Map<String, Object>> list1 = mapper.finds(map);

        Map<String,Object> map1 = new HashMap<>();
        if (null == list1 || list1.isEmpty()){
            map1.put("code",600);
            map1.put("msg","暂无数据");
        } else {
            map1.put("code",200);
            map1.put("msg","成功");
            map1.put("data",list1);

        }
        MybatisUtil.close(session);
        return map1;
    }

    @Override
    public int add(User user) {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(UserMapper.class);
        return mapper.add(user);
    }
    @Override
    public User login(String uname,String upwd) {
        //判断用户名或密码是否为空
        if (StringUtils.isNull(uname)|| StringUtils.isNull(upwd)){
            return null;
        }
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(UserMapper.class);
        User user = mapper.login(uname,upwd);
        return user;
    }
}
